package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.Result;
import com.luo.model.ResultObj;
import com.luo.model.User;
import com.luo.service.UserService;
import com.luo.util.RedisUtil;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 功能：主页商城登录
 * 作者：Luo。
 * 日期：2024/2/4 21:35
 */

@WebServlet("/userLogin")
public class UserLoginController extends HttpServlet {

	UserService userService = new UserService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json");
		ResultObj resultObj = new ResultObj();
		String account = req.getParameter("account");
		String password = req.getParameter("password");
		String phone = req.getParameter("phone");
		String code = req.getParameter("code");
		// 账号密码登录
		if (account != null && password != null) {
			//根据用户名查找该用户是否存在
			User user = userService.queryUserByUserName(account);
			//如果存在则进行密码校对
			if (user != null) {
				if (!user.getPassword().equals(password)) {
					//校对失败则提示失败
					resultObj.setCode("-2");
					resultObj.setMsg("密码错误");
					String jsonString = JSON.toJSONString(resultObj);
					resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
					resp.getWriter().write(jsonString);
					resp.getWriter().close();
					return;
				} else {
					//校对成功则提示成功
					resultObj.setCode("1");
					resultObj.setMsg("登录成功");
					UUID uuid = UUID.randomUUID();
					String token = uuid.toString().replace("-", "");
					user.setToken(token);
					//将token保存到redis数据库中
					// 连接到Redis
					Jedis jedis = RedisUtil.getJedis();
					Map<String, String> hashData = new HashMap<>();
					hashData.put("id", String.valueOf(user.getId()));
					hashData.put("currentTime", String.valueOf(System.currentTimeMillis()));
					hashData.put("token", token);
					jedis.hmset(token, hashData);
					jedis.expire(token, 3600);
					//返回到前端
					resultObj.setResult(user);
					String jsonString = JSON.toJSONString(resultObj);
					resp.getWriter().write(jsonString);
					resp.getWriter().close();
					return;
				}
			} else {
				//如果不存在则输出无此用户
				resultObj.setCode("-1");
				resultObj.setMsg("无此用户");
				String jsonString = JSON.toJSONString(resultObj);
				resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
				resp.getWriter().write(jsonString);
				resp.getWriter().close();
				return;
			}
		} else if (code != null && phone != null) {
			// 根据手机号查找用户
			User user = userService.queryUserByPhonne(phone);
			String getCode = null;
			if (user == null) {
				//如果不存在则输出无此用户
				resultObj.setCode("-1");
				resultObj.setMsg("无此用户");
				String jsonString = JSON.toJSONString(resultObj);
				resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
				resp.getWriter().write(jsonString);
				resp.getWriter().close();
				return;
			}
			// 对验证码进行查询
			Jedis jedis = RedisUtil.getJedis();
			getCode = jedis.hget(phone, "code");
			if (code.equals(getCode)){
				//校对成功则提示成功
				resultObj.setCode("1");
				resultObj.setMsg("登录成功");
				UUID uuid = UUID.randomUUID();
				String token = uuid.toString().replace("-", "");
				user.setToken(token);
				//将token保存到redis数据库中
				// 连接到Redis
				jedis = RedisUtil.getJedis();
				Map<String, String> hashData = new HashMap<>();
				hashData.put("id", String.valueOf(user.getId()));
				hashData.put("currentTime", String.valueOf(System.currentTimeMillis()));
				hashData.put("token", token);
				jedis.hmset(token, hashData);
				jedis.expire(token, 3600);
				//返回到前端
				resultObj.setResult(user);
				String jsonString = JSON.toJSONString(resultObj);
				resp.getWriter().write(jsonString);
				resp.getWriter().close();
				jedis.close();
				return;
			}else{
				//如果不存在则输出无此用户
				jedis.close();
				resultObj.setCode("-1");
				resultObj.setMsg("验证码或手机号有误");
				String jsonString = JSON.toJSONString(resultObj);
				resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
				resp.getWriter().write(jsonString);
				resp.getWriter().close();
				return;
			}
		} else {
			//如果不存在则输出无此用户
			resultObj.setCode("-1");
			resultObj.setMsg("用户信息有误");
			String jsonString = JSON.toJSONString(resultObj);
			resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			resp.getWriter().write(jsonString);
			resp.getWriter().close();
			return;
		}
	}
}
